package com.syf.kit.soa;

import com.syf.core.kit.SyFrameKit;
import com.syf.kit.soa.config.SOARegionClientConfiguration;
import com.syf.kit.soa.core.SOARegionInfo;
import com.syf.kit.soa.core.spi.SOARegionStorage;
import com.syf.kit.soa.core.spi.nacos.NacosSOAStorage;
import org.springframework.core.io.support.SpringFactoriesLoader;

import java.util.List;

@SyFrameKit("soa region存储账本")
public class SOARegionDocument {
    private SOARegionStorage soaStorage;

    public SOARegionDocument(SOARegionClientConfiguration config) {
        List<SOARegionStorage> instanceList = SpringFactoriesLoader.loadFactories(SOARegionStorage.class, this.getClass().getClassLoader());
        if (instanceList.isEmpty()) {
            // 默认为nacos
            soaStorage = new NacosSOAStorage();
        } else {
            soaStorage = instanceList.get(0);
        }

        soaStorage.loadConfig(config);
    }

    /**
     * 查找服务（自动发现）
     */
    public List<SOARegionInfo> discoveryService(String regionName) {
        return soaStorage.discoveryService(regionName);
    }

    /**
     * 注册服务
     */
    public void registerService(SOARegionInfo regionInfo) {
        soaStorage.setServiceRegionInfo(regionInfo);
        soaStorage.registerServiceRegion();
    }

    /**
     * 注销服务
     */
    public void deregisterService() {
        soaStorage.deregisterServiceRegion();
    }
}
